package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ONIE
 * @create 2021/11/25 2:26 下午
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	/**
	 * 客户列表分页
	 * @param customerVo
	 * @return
	 */
	@PostMapping("list")
	public Map<String,Object> supplierList(SearchVo customerVo){
		Integer page = customerVo.getPage();
		Integer rows = customerVo.getRows();
		String coustomerName = customerVo.getCustomerName();
		if (page == null) page = 1;
		if (rows == null) rows = 5;
		PageResult pageResult = customerService.coustomerList(page,rows,coustomerName);
		HashMap<String, Object> map = new HashMap<>();
		map.put("total",pageResult.getTotal());
		map.put("rows",pageResult.getRows());
		return map;
	}

	/**
	 * 客户添加或修改
	 * @param customerId
	 * @param customer
	 * @return
	 */
	@PostMapping("save")
	public ServiceVO save(@RequestParam(required = false) Integer customerId , Customer customer){
		customerService.saveByCoustomerId(customerId,customer);
		return new ServiceVO(100,"请求成功");
	}

	/**
	 * 客户删除
	 * @param ids
	 * @return
	 */
	@PostMapping("delete")
	public ServiceVO deleteByIds(@RequestParam String ids){
		customerService.deleteByIds(ids);
		return new ServiceVO(100,"请求成功",null);
	}
}
